/*
 * Copyright (c) 2011. c.mueller
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * The Software shall be used for Good, not Evil.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package de.altitude.cimuelle;

import org.apache.log4j.Logger;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

/**
 * Class description:
 * <p/>
 * User: cybernaut
 * Date: 11.10.11
 * Time: 20:17
 */
public class MessageBundle {

    private final static Logger LOGGER = Logger.getLogger("MessageBundle");

    private static ResourceBundle messages;

    private static final MessageBundle instance = new MessageBundle();

    private MessageBundle() {
        LOGGER.info("Initializing messagebundle internationalisation");
        initMessageBundle();
    }

    void initMessageBundle() {
        String language = "en";
        String country = "US";

        Locale currentLocale;
        currentLocale = new Locale(language, country);

        FileInputStream fis = null;
        try {
            String filename = Config.getInstance().getRessourceBundleFile();
            fis = new FileInputStream(filename);
            messages = new PropertyResourceBundle(fis);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        if (messages == null) {
            messages = ResourceBundle.getBundle("MessagesBundle", currentLocale);
        }
    }

    public static String getString(String message, HashMap<String, String> replace) {
        message = messages.getString(message);
        for (String search : replace.keySet()) {
            message = message.replace("${" + search + "}", replace.get(search));
        }
        return message;
    }

    public static String getString(String message, String search, String replace) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put(search, replace);
        return getString(message, map);
    }

    public static String getString(String message) {
        return messages.getString(message);
    }
}
